<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="视频名称" prop="resourceName">
        <el-input
            v-model="queryParams.resourceName"
            clearable
            maxlength="20"
            placeholder="请输入视频名称"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input
            v-model="queryParams.author"
            clearable
            maxlength="10"
            placeholder="请输入作者"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文件名称" prop="fileName">
        <el-input
            v-model="queryParams.fileName"
            clearable
            maxlength="20"
            placeholder="请输入文件名称"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['cockpit:media:add']"
            icon="Plus"
            plain
            type="primary"
            @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['cockpit:media:remove']"
            :disabled="multiple"
            icon="Delete"
            plain
            type="danger"
            @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="Download"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['cockpit:media:export']"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
    </el-row>

    <el-table v-loading="loading" :data="mediaList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="文件类型" prop="fileType" width="200">
        <template #default="scope">
          <span>视频</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="预览图片" prop="pic">
        <template #default="scope">
          <image-preview v-if="scope.row.pic" :src="scope.row.pic" fit="cover" preview-teleported="true"
                         style="width:50px;object-fit: cover; height:50px;"/>
          <span v-else>无</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="视频名称" prop="resourceName"/>
      <el-table-column align="center" label="作者" prop="author"/>
      <el-table-column align="center" label="文件名称" prop="fileName"/>
      <el-table-column align="center" label="上传时间" prop="createTime" width="200"/>
      <el-table-column align="center" label="上传人" prop="createBy" width="200"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作" width="200">
        <template #default="scope">
          <el-button
              v-hasPermi="['cockpit:media:edit']"
              icon="View"
              type="text"
              @click="handlePreview(scope.row)"
          >预览
          </el-button>
          <el-button
              v-hasPermi="['cockpit:media:edit']"
              icon="Edit"
              type="text"
              @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
              v-hasPermi="['cockpit:media:remove']"
              icon="Delete"
              type="text"
              @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.pageNum"
        :total="total"
        @pagination="getList"
    />

    <!-- 添加或修改多媒体信息对话框 -->
    <el-dialog v-model="open" :title="title" append-to-body width="500px">
      <el-form ref="mediaRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item v-if="!form.id" label="视频文件" prop="file">
          <FileUpload ref="fileUpload"
                      :fileList="form.file"
                      :fileType="['mp4']"
                      :limit="1"
                      accept=".mp4"
                      @getFileList="getFileList">
          </FileUpload>
        </el-form-item>
        <el-form-item label="视频名称" prop="resourceName">
          <el-input v-model="form.resourceName" maxlength="20" placeholder="请输入视频名称" show-word-limit
                    type="text"/>
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input v-model="form.author" maxlength="10" placeholder="请输入作者" show-word-limit type="text"/>
        </el-form-item>
        <el-form-item label="预览图片" prop="pic" style="">
          <image-upload v-model="form.pic" :limit="1"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" maxlength="100" placeholder="请输入备注" show-word-limit type="textarea"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <videoImagePreview ref="videoPlayerRef"/>
  </div>
</template>

<script name="Media" setup>
import {addMedia, delMedia, getMedia, listMedia, updateMedia} from "@/api/media/media";
import FileUpload from "@/components/FileUpload/index.vue";
import videoImagePreview from "@/components/videoImagePreview";
import {ref} from "vue";

const {proxy} = getCurrentInstance();

const mediaList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const videoPlayerRef = ref();

const data = reactive({
  form: {
    fileType: 2,
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    fileName: null,
    fileType: 2,
    path: null,
    author: null,
    resourceName: null,
    like: null,
  },
  rules: {
    resourceName: [
      {
        required: true,
        message: "音乐名称不能为空",
        trigger: "change"
      },
      {
        whitespace: true,
        message: '不可以只存在空格',
        trigger: 'blur'
      },
    ],
    author: [
      {
        required: true,
        message: "作者不能为空",
        trigger: "change"
      },
      {
        whitespace: true,
        message: '不可以只存在空格',
        trigger: 'blur'
      },
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询多媒体信息列表 */
function getList() {
  loading.value = true;
  listMedia(queryParams.value).then(response => {
    mediaList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    file: [],
    fileType: 2,
    path: null,
    playMode: null,
    process: null,
    like: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  };
  if (proxy.$refs["fileUpload"]) {
    proxy.$refs["fileUpload"].clearFile();
  }
  proxy.resetForm("mediaRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加视频文件";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value
  getMedia(id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改视频文件";
  });
}

/** 提交按钮 */
function submitForm() {
  if (!form.value.path) {
    proxy.$modal.msgError("文件不能为空，请上传文件");
    return
  }
  proxy.$refs["mediaRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateMedia(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMedia(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const ida = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除该视频数据？').then(function () {
    return delMedia(ida);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('cockpit/media/export', {
    ...queryParams.value
  }, `media_${new Date().getTime()}.xlsx`)
}

function getFileList(file) {
  form.value.fileName = file.originalFilename;
  form.value.path = file.fileName;
}

/** 预览按钮 */
function handlePreview(row) {
  videoPlayerRef.value.openDialog("MP4", import.meta.env.VITE_APP_BASE_API + row.path, row.id);
}

getList();
</script>
